﻿Imports System
Imports System.Data
Imports System.Configuration
Imports System.Collections
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports Microsoft.Reporting.WebForms
Imports log4net

Partial Class Report_RP11
    Inherits System.Web.UI.Page

    Public ScriptText As String
    Dim Report As Object
    Private Shared logger As ILog = LogManager.GetLogger("Report_RP11")

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init

        'If Session("fundcenterS") = "" Then
        '    Session("fundcenterS") = "0011"
        '    Session("fundcenterE") = "0012"

        'End If

        'If Session("users") = "" Then
        '    Session("users") = "wassana"
        'End If
        'Session("year") = 2556
        'Session("version") = 2


        Getdata()


    End Sub


    Function Getdata() As Decimal


        Dim adapter As New Data_Report2TableAdapters.RP11TableAdapter
        Dim sql As String
        Dim dt As New Data_Report2.RP11DataTable


        sql = " SELECT BudgetDetails.[Id],BudgetDetails.[PlanYear],BudgetDetails.[Version],BudgetDetails.[Amount],CASE ISNULL(BudgetDetails.[ActivityType_ActivityTypeCode],'') "
        sql += " WHEN '' THEN (SELECT TOP 1 [AppConfigs].[Description] FROM [AppConfigs] "
        sql += " WHERE [AppConfigs].Name = 'ASSET_GROUP' AND [AppConfigs].[Value] = BudgetDetails.AssetRowNo )"
        sql += " ELSE (SELECT TOP 1 [AppConfigs].[Description] FROM [AppConfigs] "
        sql += " WHERE [AppConfigs].Name = 'ASSET_GROUP' AND [AppConfigs].[Value] = ActivityTypes.AssetGroup )"
        sql += " END AS GroupName,BudgetDetails.[ActivityType_PlanYear]"
        sql += " ,CASE ISNULL(BudgetDetails.[ActivityType_ActivityTypeCode],'') WHEN '' THEN BudgetDetails.[AssetNo] ELSE BudgetDetails.[ActivityType_ActivityTypeCode] END"
        sql += " AS [ActivityType_ActivityTypeCode],CASE ISNULL(BudgetDetails.[ActivityType_ActivityTypeCode],'') WHEN '' THEN BudgetDetails.[AssetDescription] ELSE ActivityTypes.Description END"
        sql += " AS ActTypeName,STUFF((SELECT ',' + FundedProgram FROM FundedProgramAssets"
        sql += " WHERE FundedProgramAssets.BudgetDetail_Id = BudgetDetails.[Id] ORDER BY FundedProgram  FOR XML PATH('')),1,1,'') AS IntOrder"
        sql += " ,BudgetDetails.[Activity_ActivityCode],BudgetDetails.[Activity_PlanYear],Activities.Description AS ActivityName,BudgetDetails.[Product_ProductCode]"
        sql += " ,BudgetDetails.[Product_PlanYear],BudgetDetails.[FundCenter_FundCenterCode],BudgetDetails.[FundCenter_PlanYear],Fundcenters.Description AS FundCenterName"
        sql += "  ,BudgetDetails.[Project_ProjectCode] ,BudgetDetails.[Project_PlanYear],BudgetDetails.[Project_FundCenterCode],Projects.Description AS ProjectName"
        sql += " ,BudgetDetails.[CmmtDetail_Id],BudgetDetails.[Fund_FundCode] ,BudgetDetails.[Fund_PlanYear],BudgetDetails.[Cmmt_CmmtCode],BudgetDetails.[Cmmt_PlanYear]"
        sql += "  ,Budget12.[Id] as B12ID,Budget12.[Amount1],Budget12.[Amount2],Budget12.[Amount3],Budget12.[Amount4],Budget12.[Amount5],Budget12.[Amount6]"
        sql += " ,Budget12.[Amount7],Budget12.[Amount8],Budget12.[Amount9],Budget12.[Amount10],Budget12.[Amount11],Budget12.[Amount12] ,Cmmts.Description as cmmt_name"
        sql += " ,CASE ISNULL(BudgetDetails.[Project_ProjectCode],'') WHEN '' THEN 'งบประมาณประจำ' ELSE"
        sql += " CASE LEFT(ISNULL(BudgetDetails.[Project_ProjectCode],''),1) WHEN 'P' THEN 'งบประมาณโครงการ' ELSE 'งบประมาณสนับสนุน' END END AS BudgetType"
        sql += " FROM BudgetDetails INNER JOIN Budget12 ON BudgetDetails.Id = Budget12.[BudgetDetail_Id]"
        sql += " INNER JOIN Cmmts ON BudgetDetails.[Cmmt_CmmtCode]=Cmmts.[CmmtCode] and BudgetDetails.[PlanYear]=Cmmts.[PlanYear]"
        sql += " INNER JOIN FundCenters ON BudgetDetails.[FundCenter_FundCenterCode] = Fundcenters.FundCenterCode AND BudgetDetails.[FundCenter_PlanYear] = FundCenters.PlanYear"
        sql += " LEFT OUTER JOIN Activities ON BudgetDetails.[Activity_ActivityCode] = Activities.ActivityCode AND BudgetDetails.[Activity_PlanYear] = Activities.PlanYear"
        sql += " LEFT OUTER JOIN Projects ON BudgetDetails.[Project_ProjectCode] = Projects.ProjectCode AND BudgetDetails.[Project_PlanYear] = Projects.PlanYear"
        sql += " AND BudgetDetails.[Project_FundCenterCode] = Projects.FundCenter_FundCenterCode"
        sql += " LEFT OUTER JOIN ActivityTypes ON BudgetDetails.[ActivityType_ActivityTypeCode] = ActivityTypes.ActivityTypeCode"
        sql += " AND BudgetDetails.[ActivityType_PlanYear] = ActivityTypes.PlanYear WHERE BudgetDetails.FormId = 'F04'"



        sql += " and  BudgetDetails.PlanYear  = " & Session("year") & " and BudgetDetails.Version = '" & Session("version") & "'"
        If Session("FCTRmode") = "1" Then
            sql += " and  BudgetDetails.FundCenter_FundCenterCode IN (" & Session("FundcenterAll") & ") "
        ElseIf Session("FCTRmode") = "2" Then
            sql += " and  BudgetDetails.FundCenter_FundCenterCode IN (" & Session("FundcenterAll") & ") "
        End If


        'If Val(Session("ActivityS")) <> 0 And Val(Session("ActivityE")) <> 0 Then
        '    sql += "   and  BudgetDetails.ActivityType_ActivityTypeCode between " & Session("ActivityS") & " And " & Session("ActivityE")

        'End If
        'Add by Orn 18/08/2558
        If Session("ActivityTypeS") <> "" Then
            sql += " and BudgetDetails.ActivityType_ActivityTypeCode >= " & Session("ActivityTypeS")
        End If
        If Session("ActivityTypeE") <> "" Then
            sql += " and BudgetDetails.ActivityType_ActivityTypeCode <= " & Session("ActivityTypeE")
        End If

        'Response.Write(sql)
        'Exit Function
        adapter.GetDataByFilter(dt, sql)



        'Return 1
        'Exit Function
        dt.TableName = "R11DT"
        Dim das As New DataSet
        das.DataSetName = "R01DS"
        das.Tables.Add(dt)
        ReportViewer1.ProcessingMode = Microsoft.Reporting.WebForms.ProcessingMode.Local
        'ReportViewer1.LocalReport.ReportPath = System.Environment.CurrentDirectory & "Report\R01.rdlc"
        ReportViewer1.LocalReport.DataSources.Clear()
        ReportViewer1.LocalReport.DataSources.Add(New Microsoft.Reporting.WebForms.ReportDataSource("DataSet1", das.Tables("R11DT")))
        ReportViewer1.DocumentMapCollapsed = True

        Dim UserName As String = Session("users")
        Dim years As String = Session("year")
        Dim version As String = Session("version")
        Dim users As String = Session("users")

        Dim p1, p2, p3 As ReportParameter
        p1 = New ReportParameter("year", years)
        p2 = New ReportParameter("version", version)
        p3 = New ReportParameter("username", UserName)

        ReportViewer1.LocalReport.SetParameters(p1)
        ReportViewer1.LocalReport.SetParameters(p2)
        ReportViewer1.LocalReport.SetParameters(p3)


        ReportViewer1.LocalReport.Refresh()


        'Response.Write(dt.Rows.Count & sql)
        Session("count") = dt.Rows.Count



        Return dt.Rows.Count

    End Function


    Public Function nPageNumber() As String
        Dim str As String
        str = Me.Report.Globals!PageNumber.ToString()
        Return str
    End Function

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Session("users") Is Nothing Then
            ScriptText = "<script> wclose();</script>"
        End If
    End Sub
End Class
